﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Model.Result;
using Estsh.Core.Repositories;
using System.Collections;
using System.Data;
using System.Text;
using System.Security.Cryptography;
using System.Text.Json;
using Estsh.Core.Models;
using Newtonsoft.Json.Linq;
using Estsh.Core.Model.EnumUtil;

namespace Estsh.Core.Wms.Repositories
{
    public class BomRepository : BaseRepository<SysBom>, IBomRepository
    {
        public BomRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {

        }
        /// <summary>
        /// 根据零件回冲Bom信息
        /// </summary>
        /// <returns></returns>
        public List<SysBomDetail> GetReportBomInfo(string partNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                if (dbConn.State == ConnectionState.Closed)
                {
                    dbConn.Open();
                }

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT b.*,a.start_ymd FROM dbo.sys_bom a (nolock) ");
                sql.Append("    JOIN dbo.sys_bom_detail b (nolock) ON a.bom_id = b.bom_id ");
                sql.Append("WHERE a.part_no = @partNo ");
                sql.Append("      AND a.start_ymd < @startYmd ");
                sql.Append("      AND a.end_ymd > @endYmd ");
                sql.Append("      AND a.enabled = 'Y' ");
                sql.Append("	  AND a.bom_type = @bomType ");

                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@partNo", partNo);
                parameters.Add("@startYmd", DateTime.Now.ToString("yyyy-MM-dd"));
                parameters.Add("@endYmd", DateTime.Now.ToString("yyyy-MM-dd"));
                parameters.Add("@bomType", MesEnumUtil.BomType.REPORT);

                return dbConn.Query<SysBomDetail>(sql.ToString(), parameters).ToList();
            }
        }
    }
}
